package com.party.service.exam.impl;

import com.party.common.Page;
import com.party.mapper.ExamAnswerMapper;
import com.party.mapper.ExamAnswerMapper;
import com.party.model.ExamAnswer;
import com.party.model.ExamAnswer;
import com.party.model.ExamAnswerExample;
import com.party.service.exam.ExamAnswerService;
import com.party.service.exam.ExamAnswerService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ExamAnswerServiceImpl implements ExamAnswerService {

    @Resource
    ExamAnswerMapper examAnswerMapper;

    @Override
    public int addExamAnswer(ExamAnswer examAnswer) {
        int num = examAnswerMapper.insertSelective(examAnswer);
        return num;
    }

    @Override
    public int deleteExamAnswerByPrimaryKey(Integer id) {
        int num = examAnswerMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += examAnswerMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public int deleteExamByExamTestIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += examAnswerMapper.deleteByPrimaryKey(Integer.parseInt(roid));

                ExamAnswerExample examExample = new ExamAnswerExample();
                ExamAnswerExample.Criteria criteria = examExample.createCriteria();
                criteria.andTestidEqualTo(Integer.parseInt(roid));
                examAnswerMapper.deleteByExample(examExample);
            }
        }
        return num;
    }

    @Override
    public ExamAnswer getExamAnswerByPrimaryKey(Integer id) {
        ExamAnswer exam = examAnswerMapper.selectByPrimaryKey(id);
        return exam;
    }

    @Override
    public int updateExamAnswerByPrimaryKey(ExamAnswer ExamAnswer) {
        int num = examAnswerMapper.updateByPrimaryKeySelective(ExamAnswer);
        return num;
    }

    @Override
    public List<ExamAnswer> getListExamAnswerByExamTestPrimaryKey(Integer examTestId) {
        List<ExamAnswer> listExamAnswer=examAnswerMapper.getListExamAnswerByExamTestPrimaryKey(examTestId);
        return listExamAnswer;
    }

    @Override
    public Page getExamAnswerTestList(ExamAnswer examAnswer, int pageSize, int pageNumber) {
        ExamAnswerExample examAnswerExample = new ExamAnswerExample();
        /**
         * 可以添加查询条件
         */
        if(examAnswer != null)
        {
            ExamAnswerExample.Criteria criteria = examAnswerExample.createCriteria();
            if(examAnswer.getContent() != null && !"".equals(examAnswer.getContent()))
            {
                criteria.andContentLike("%"+examAnswer.getContent()+"%");
            }
        }

        //获取总的记录数量
        int count = examAnswerMapper.countByExample(examAnswerExample);
        //获取指定的页码数据
        examAnswerExample.setBeginNum((pageNumber - 1) * pageSize);
        examAnswerExample.setEndNum(pageSize);
        List<ExamAnswer> list = examAnswerMapper.getListPage(examAnswerExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public List<ExamAnswer> getByTestId(Integer examtestid) {
        if(examtestid != null && examtestid > 0)
        {
            ExamAnswerExample examAnswerExample = new ExamAnswerExample();
            ExamAnswerExample.Criteria criteria = examAnswerExample.createCriteria();
            criteria.andTestidEqualTo(examtestid);
            return this.examAnswerMapper.selectByExample(examAnswerExample);
        }
        return null;
    }
}
